//
//  NSString+OurString.m
//  DSLC
//
//  Created by gcct on 2016/11/11.
//  Copyright © 2016年 孙海琛. All rights reserved.
//

#import "NSString+OurString.h"

@implementation NSString (OurString)
+(NSString *)versionName
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}
+ (NSString *)getDeviceId
{
    NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
    return identifierForVendor;
}
/*
 * 改变字符串其中某一个范围内文字颜色以及大小
 * ourString：字符串全部内容
 * leftRange：从左开始需要改变的范围(有两个参数第一个是开始截取位置第二个参数是截取长度)
 * rightRange：从右开始需要改变的范围(有两个参数第一个是开始截取位置第二个参数是截取长度)
 * font：改变文字的大小
 * color：改变文字的颜色
 */
+(NSMutableAttributedString *)ourString:(NSString *)ourString changeStringleftRange:(NSRange)leftRange changeStringRightRange:(NSRange)rightRange changeStringFont:(CGFloat)font changeStringColor:(UIColor *)color
{
    NSMutableAttributedString *moneyString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",ourString]];
    [moneyString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:leftRange];
    [moneyString addAttribute:NSForegroundColorAttributeName value:color range:leftRange];
    [moneyString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:rightRange];
    [moneyString addAttribute:NSForegroundColorAttributeName value:color range:rightRange];
    return moneyString;
}
- (BOOL)containString:(NSString *)string {
    NSRange range = [self rangeOfString:string];
    return range.length > 0 ? YES : NO;
}
+(NSMutableAttributedString *)lableJianJu:(NSString *)contents lineSpacing:(float)line
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contents];
    NSMutableParagraphStyle   *paragraphStyle   = [[NSMutableParagraphStyle alloc] init];
    
    //行间距
    [paragraphStyle setLineSpacing:line];
    //段落间距
//    [paragraphStyle setParagraphSpacing:10.0];
    //第一行头缩进
//    [paragraphStyle setFirstLineHeadIndent:15.0];
    //头部缩进
    //[paragraphStyle setHeadIndent:15.0];
    //尾部缩进
    //[paragraphStyle setTailIndent:250.0];
    //最小行高
    //[paragraphStyle setMinimumLineHeight:20.0];
    //最大行高
    //[paragraphStyle setMaximumLineHeight:20.0];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contents length])];
    return attributedString;
}
+ (CGSize)yx_calculateStringSize:(NSString *)str font:(UIFont *)font widthOrHeight:(CGFloat)fixedSize isWidth:(BOOL)isWidth{
    
    CGSize   sizeC ;
    
    if (isWidth) {
        sizeC = CGSizeMake(fixedSize ,MAXFLOAT);
    }else{
        sizeC = CGSizeMake(MAXFLOAT ,fixedSize);
    }
    
    CGSize   sizeFileName = [str boundingRectWithSize:sizeC
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:font}
                                              context:nil].size;
    
    return sizeFileName;
}

/**
 添加中划线
 
 @param str str 入参
 @return 出参
 */
+(NSMutableAttributedString *)addLineString:(NSString *)str{
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:str attributes:attribtDic];
    
    return attribtStr;
}
//添加下划线
+(NSMutableAttributedString *)addDownLineString:(NSString *)str color:(UIColor *)color
{
    NSMutableAttributedString * att_str2 = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange strRange = {0,[att_str2 length]};
    [att_str2 addAttribute:NSForegroundColorAttributeName value:color range:strRange];
    [att_str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    return att_str2;
}
/**
 改变字符串中￥大小
 
 @param str 入参
 @return 出参
 */
+(NSMutableAttributedString *)updateMoneyString:(NSString *)str rangeFont:(NSInteger)size
{
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:str];
    
    if ([str containString:@"赚"]) {
        NSRange redRangeOne = NSMakeRange([str rangeOfString:@"赚"].location+1, 1);
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeOne];
    }
    if ([str containString:@"省"]) {
        NSRange redRangeTwo = NSMakeRange([str rangeOfString:@"省"].location+1, 1);
        
        [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeTwo];
    }
    
    
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,str.length)];
    return noteStr;
}


/**
 改变字符串中￥大小以及金额.后面大小
 
 @param str 入参
 @return 出参
 */
+(NSMutableAttributedString *)updateString:(NSString *)str rangeFont:(NSInteger)size
{
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",str]];
    
    NSRange redRangeOne = NSMakeRange([[noteStr string] rangeOfString:@"￥"].location, [[noteStr string] rangeOfString:@"￥"].length);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeOne];
    
    NSRange redRangeTwo = NSMakeRange(noteStr.length-2, 2);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeTwo];
    
    return noteStr;
}

+(NSMutableAttributedString *)addLineString:(NSString *)str rangeFont:(NSInteger)size
{
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",str] attributes:attribtDic];
    
    NSRange redRangeOne = NSMakeRange([[noteStr string] rangeOfString:@"￥"].location, [[noteStr string] rangeOfString:@"￥"].length);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeOne];
    
    NSRange redRangeTwo = NSMakeRange(noteStr.length-2, 2);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeTwo];
    
    return noteStr;
}

+(NSMutableAttributedString *)updateString:(NSString *)str :(NSString *)s rangeFont:(NSInteger)size
{
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@￥%@",s,str]];
    
    NSRange redRangeOne = NSMakeRange([[noteStr string] rangeOfString:@"￥"].location, [[noteStr string] rangeOfString:@"￥"].length);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeOne];
    
    NSRange redRangeTwo = NSMakeRange(noteStr.length-2, 2);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeTwo];
    
    return noteStr;
}

+(NSMutableAttributedString *)updateS:(NSString *)str rangeFont:(NSInteger)size
{
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",str]];
    
    NSRange redRangeOne = NSMakeRange([[noteStr string] rangeOfString:@"￥"].location, [[noteStr string] rangeOfString:@"￥"].length);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeOne];
    
    NSRange redRangeTwo = NSMakeRange(0, 1);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeTwo];
    
    return noteStr;
}

+(NSMutableAttributedString *)updateS:(NSString *)str :(NSString *)s rangeFont:(NSInteger)size
{
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@￥%@",s,str]];
    
    NSRange redRangeOne = NSMakeRange([[noteStr string] rangeOfString:@"￥"].location, [[noteStr string] rangeOfString:@"￥"].length);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeOne];
    
    NSRange redRangeTwo = NSMakeRange(0, 1);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Medium" size:size] range:redRangeTwo];
    
    return noteStr;
}

+(NSMutableAttributedString *)updateScoreString:(NSString *)str rangeFont:(NSInteger)size{
//    if (str == nil) {
//        str = @"5.0";
//    }
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:str];
    
    NSRange redRangeTwo = NSMakeRange(noteStr.length-1, 1);
    
    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:redRangeTwo];
    
    return noteStr;
}
+ (NSMutableAttributedString *)updateCommentNumLabel:(NSString*) price{
    NSMutableAttributedString * noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"查看全部评价(%@)",price]];
    
    NSRange redRangeOne = NSMakeRange([[noteStr string] rangeOfString:@"("].location,noteStr.length - [[noteStr string] rangeOfString:@"("].location);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorFromHexCode:@"de8911"] range:redRangeOne];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:redRangeOne];
    NSRange redRangeTwo = NSMakeRange(0,[[noteStr string] rangeOfString:@"("].location - 0);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorFromHexCode:@"888888"] range:redRangeTwo];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:redRangeTwo];
    return noteStr;
}
+ (NSMutableAttributedString *)setLableText:(NSString *)text setLineSpacing:(NSInteger)num
{
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle  setLineSpacing:num];
    NSMutableAttributedString  *setString = [[NSMutableAttributedString alloc] initWithString:text];
    [setString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    return setString;
}
+ (NSString *)yx_timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp/1000.0];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}
+ (BOOL)isValidMobile:(NSString *)phoneNum{
    
    // 验证用户名 - 手机号码
    NSString *regEx = @"^1[3|4|5|7|8][0-9]\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];
    
    return [phoneTest evaluateWithObject:phoneNum];
}
+ (NSString *)phoneSuit:(NSString *)phone
{
    if ([NSString isValidMobile:phone]) {
        NSString *numberString = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        return numberString;
    }
    return phone;
}

+(NSString *)setNumberPrice:(NSString *)numP
{
    float num = numP.floatValue;
    num = num/100;
    NSString *strNum = [NSString stringWithFormat:@"%.2f",num];
    return strNum;
}
+(NSString *)setNumberNoPointPrice:(NSString *)numP
{
    float num = numP.floatValue;
    num = num/100;
    NSString *strNum = [NSString stringWithFormat:@"%.0f",num];
    return strNum;
}
//计算金额
+ (NSString *)addNumPrice:(NSString *)numP mainPrice:(NSString *)mainS addOurOff:(NSString *)addOff
{
    float ourPrice = mainS.floatValue;
    float num = numP.floatValue;
    if ([addOff isEqualToString:@"+"]) {
        ourPrice = ourPrice+num;
    }else
    {
        ourPrice = ourPrice-num;
    }
    return [NSString stringWithFormat:@"%.f",ourPrice];
}

+(NSString *)codePhoneNum:(NSString *)phoneNum{
    
    NSString *codePhone = [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    
    return codePhone;
}

+ (BOOL)includeChinese:(NSString *)str
{
    for(int i=0; i< [str length];i++)
    {
        int a =[str characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}
/**
 按特殊字符分割字符串
 */
+ (NSArray *)segmenString:(NSString *)str chara:(NSString *)charStr
{
    if ([str isEqualToString:@""] || str == nil) {
        return nil;
    }else
    {
        NSArray *array = [str componentsSeparatedByString:charStr];
        NSMutableArray *arrR = [NSMutableArray arrayWithArray:array];
        if ([arrR.lastObject isEqualToString:@""]) {
            [arrR removeObject:arrR.lastObject];
        }
        return arrR;
    }
}
//按逗号分隔图片字符串返回第一个图片地址
+ (NSString *)imgUrlByString:(NSString *)strUrl
{
    NSArray *arrImg = [strUrl componentsSeparatedByString:@","];
    if ([NSString includeChinese:arrImg[0]]) {
        NSString* encodedString = [arrImg[0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return encodedString;
    }else
    {
        return arrImg[0];
    }
}
//判断链接是否包含中文
+ (NSURL *)isChinese:(NSString *)str
{
    if ([NSString includeChinese:str]) {
        NSString* encodedString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return encodedString;
    }else
    {
        return str;
    }
}

+ (BOOL)stringISNil:(NSString *) str{
    return str == nil || [str isEqualToString:@""]?YES:NO;
}

//判断字符串是否为nil(如果为nil 返回@"")
+ (NSString *)strIsNil:(NSString *)str
{
    if (str == nil || [str isKindOfClass:[NSNull class]]) {
        return @"";
    }else
    {
        return str;
    }
}

+ (NSAttributedString *)htmlString:(NSString *)str
{
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    return attrStr;
}

//跳转appStore
+ (void)goAppStore
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/app/apple-store/id1356026239?ct=web&mt=8"]];
}

//是否包含中文
- (BOOL)isChinese
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:self];
}
@end
